/**
 * HanziWriter Wrapper for UniApp
 * This file provides a wrapper around the HanziWriter library to make it work in UniApp environment
 */

// Import the HanziWriter script
import '../static/js/hanzi-writer.min.js';

// Get the HanziWriter object from the global scope
let HanziWriterInstance = null;

// Initialize HanziWriter based on the environment
function getHanziWriter() {
  if (HanziWriterInstance) {
    return HanziWriterInstance;
  }

  try {
    // Try to get from global scope (browser environment)
    if (typeof window !== 'undefined' && window.HanziWriter) {
      HanziWriterInstance = window.HanziWriter;
      return HanziWriterInstance;
    }
    
    // Try to get from global data (UniApp environment)
    if (typeof getApp === 'function') {
      const app = getApp();
      if (app && app.globalData && app.globalData.HanziWriter) {
        HanziWriterInstance = app.globalData.HanziWriter;
        return HanziWriterInstance;
      }
    }
    
    // If we can't find it, try to access it directly
    if (typeof HanziWriter !== 'undefined') {
      HanziWriterInstance = HanziWriter;
      return HanziWriterInstance;
    }
    
    console.error('HanziWriter not found in any scope');
    return null;
  } catch (error) {
    console.error('Error initializing HanziWriter:', error);
    return null;
  }
}

export default getHanziWriter(); 